package com.scsj.supermarket.view.activity.settingmodel;

import android.os.Handler;
import android.os.Message;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.scsj.supermarket.R;
import com.scsj.supermarket.utils.SkipUtils;
import com.scsj.supermarket.utils.ToastMiddleUtils;
import com.scsj.supermarket.view.activity.baseactivitymodel.BaseActivity;

/**
 * 找回密码，验证码验证手机号
 */
public class PasswordVerificationCodeActivity extends BaseActivity implements View.OnClickListener {

    private TextView next_step_tv, send_verification_tv;
    private EditText note_verification_code_et;
    //时间倒计时
    private int second = 120;

    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (msg.what == 1) {
                second -= 1;
                if (second == 0) {
//                    send_verification_tv.setBackgroundResource(R.drawable.verification_code_gray_side_line);
                    removeMessages(1);
                    send_verification_tv.setEnabled(true);
                    send_verification_tv.setText("获取验证码");
                    second = 120;
                    send_verification_tv.setTextColor(getResources().getColor(R.color.credit_text_gray));
//                    verification_count_down_tv.setTextColor(0xffe74d26);
                } else {
                    send_verification_tv.setText(second + "秒后重发");
                    handler.sendEmptyMessageDelayed(1, 1000);
                }
            }
        }
    };

    @Override
    protected void createView(Bundle savedInstanceState) {
        setContentView(R.layout.activity_password_verification_code);
        initToolbar();
        setToolbarTitle("找回密码");
        hasBack(true);
        ToastMiddleUtils.ToastMiddle(this, "请输入130****5277收到的\n短信验证码");
    }

    @Override
    protected void initView() {
        next_step_tv = findViewById(R.id.next_step_tv);
        send_verification_tv = findViewById(R.id.send_verification_tv);
        note_verification_code_et = findViewById(R.id.note_verification_code_et);
    }

    @Override
    protected void initListener() {
        next_step_tv.setOnClickListener(this);
        send_verification_tv.setOnClickListener(this);
        note_verification_code_et.addTextChangedListener(mEditText);
    }

    @Override
    protected void initData() {

    }

    /**
     * 验证手机号并获取验证码
     */
    private void GetTheVerifyingCode() {
        StartTimer();
    }

    private void StartTimer() {
        send_verification_tv.setText(second + "秒后重发");
        send_verification_tv.setTextColor(getResources().getColor(R.color.credit_text_gray));
//        send_verification_tv.setBackgroundResource(R.drawable.verification_code_gray_side_line);
        send_verification_tv.setEnabled(false);
        handler.sendEmptyMessageDelayed(1, 1000);
    }

    TextWatcher mEditText = new TextWatcher() {
        private CharSequence temp;

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            temp = s;
        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count,
                                      int after) {
//            adb_password.setVisibility(View.VISIBLE);
//            adb_lin2.setVisibility(View.GONE);
        }

        @Override
        public void afterTextChanged(Editable s) {
            if (note_verification_code_et.getText().length() == 6) {
//                ToastMiddleUtils.ToastMiddle(PasswordVerificationCodeActivity.this,"");
                next_step_tv.setBackgroundResource(R.color.unify_red);
            }
        }
    };

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.next_step_tv:
                if (note_verification_code_et.getText().equals("")) {
                    ToastMiddleUtils.ToastMiddle(this, "请输入130****5277收到的\n短信验证码");
                } else {
                    SkipUtils.toSetNewPassword(this);
                }

                break;
            case R.id.send_verification_tv:
                GetTheVerifyingCode();
                break;
        }
    }
}
